#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/*TC1100 Gamma寄存器位址*///(0->255)(PAGE+OFFSET)
u8 TC1100_Gamma_r[48]={0x0F,0x68,0x0F,0x69,0x0F,0x6B,0x0F,0x6C,0x0F,0x6D,0x0F,0x6E,0x0F,0x6F,0x0F,0x70,0x0F,0x71,0x0F,0x72,0x0F,0x73,0x0F,0x74,0x0F,0x75,0x0F,0x76,0x0F,0x77,0x0F,0x78,0x0f,0x79,0x0f,0x7a,0x0f,0x7b,0x0f,0x7c,0x0f,0x7d,0x0f,0x7e,0x0f,0x7f,0x0b,0xff};
u8 TC1100_Gamma_g[48]={0x10,0xe9,0x10,0xea,0x10,0xeb,0x10,0xec,0x10,0xed,0x10,0xee,0x10,0xef,0x10,0xf0,0x10,0xf1,0x10,0xf2,0x10,0xf3,0x10,0xf4,0x10,0xf5,0x10,0xf6,0x10,0xf7,0x10,0xf8,0x10,0xf9,0x10,0xfa,0x10,0xfb,0x10,0xfc,0x10,0xfd,0x10,0xfe,0x10,0xff,0x0c,0xff};
u8 TC1100_Gamma_b[48]={0x12,0x69,0x12,0x6a,0x12,0x6b,0x12,0x6c,0x12,0x6d,0x12,0x6e,0x12,0x6f,0x12,0x70,0x12,0x71,0x12,0x72,0x12,0x73,0x12,0x74,0x12,0x75,0x12,0x76,0x12,0x77,0x12,0x78,0x12,0x79,0x12,0x7a,0x12,0x7b,0x12,0x7c,0x12,0x7d,0x12,0x7e,0x12,0x7f,0x0d,0xff};



/********************************************************************************************
********************************************************************************************
    TC1100  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    TC1100  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    TC1100  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    TC1100  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    TC1100  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void TC1100_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    //SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data&0xFBFF)|0x0400); 
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);//HS mode&DCS long package	write	
    delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok
}
void TC1100_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
		u8 buffer1[5];
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);   //general mode read                            
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]-2); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	
    buffer1[0] = 0x02;
    buffer1[2] = buffer[3]; 	//Page	
    buffer1[3] = buffer[4]; 	//offset	
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-2;i++)  //data read out length
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[5+i]=tmp>>8;
        buffer[6+i]=tmp;
        delay_ms(5);
        i++;
    }
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}
void TC1100_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
		u8 i=0,m=0;
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
    delay_ms(5);
		for(i=4;i<buffer[2]+4;i++) //gamma数据从buffer4 开始
		{
			if(buffer[3]==0x01)//red
			{
				buffer1[0] = 0x04;
				buffer1[1] = 0xDF; 	//Page	
				buffer1[2] = 0xE0+TC1100_Gamma_r[m]; 	//Page	
				buffer1[3] = TC1100_Gamma_r[m+1]; 	//offset	
				buffer1[4] = buffer[i]; 	
				SSD2828_W_Array(SigMode,channel,buffer1,0);
			}
			else if(buffer[3]==0x02)//green
			{
				buffer1[0] = 0x04;
				buffer1[1] = 0xDF; 	//Page	
				buffer1[2] = 0xE0+TC1100_Gamma_g[m]; 	//Page	
				buffer1[3] = TC1100_Gamma_g[m+1]; 	//offset	
				buffer1[4] = buffer[i]; 	
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
			}
			else if(buffer[3]==0x03)//blue
			{
				buffer1[0] = 0x04;
				buffer1[1] = 0xDF; 	//Page	
				buffer1[2] = 0xE0+TC1100_Gamma_b[m];  	//Page	
				buffer1[3] = TC1100_Gamma_b[m+1]; 	//offset	
				buffer1[4] = buffer[i]; 	
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
			}
			m+=2;
		}

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  	

}
void TC1100_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[6];
		SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data&0xFBFF)|0x0400); 	
		delay_ms(15);

    buffer1[0] = 0x02;
    buffer1[1] = 0xE0; 
    buffer1[2] = 0x11;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 	
		delay_ms(1234);	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 		
}

void ProcessForIc31( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
			case 0x01:                                      //写寄存器
					TC1100_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
					break;
			case 0x02:                                      //读寄存器
					TC1100_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
					break;
			case 0x04:                                      //写Gamma
					TC1100_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
					break;
			case 0x05:                                      //读Gamma
					TC1100_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
			break;
			default:break;
	}			
}


